<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;


class Fansfollow extends Controller
{

    // 关注页面
    public function index()
    {
        // 获取session
        $session = Session::get();

        // 获取id
        $id = $session['hmy_user']['id'];

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询关注

        $follow = Db::table(['follow' =>'f','hmy_user' =>'u','users_follow'=>'uf'])
                    ->where('f.id = uf.fid && u.id = uf.uid && uf.uid = '.$id)
                    ->field('f.iconname,f.sex,f.icon,u.status,f.user_id,f.id')
                    ->select();


        // 关注数量
        $follow_num = count($follow);

        // 返回
        return view('index@fansfollow/follow',[
            'session'       => $session,
            'category'      => $category,
            'follow'        => $follow,
            'follow_num'    => $follow_num

        ]);

    }


    // 加入关注
    public function concern()
    {
        // 接收对方id
        $user_id = input('uid');

        // 获取session
        $session = Session::get('hmy_user');

        // 判断是否登录
        if ($session == null) {
           
            $info['status'] = false;
            $info['msg'] = '您还没有登录,请先登录!';

            return json($info);
        }

        // 获取自己的id
        $sess_id = $session['id'];

        // 判断是否是关注自己的
        if ($user_id == $sess_id) {
           
            $info['status'] = false;
            $info['msg'] = '不能关注自己!';

            return json($info);
        }


        // 获取对方的信息
        $user = DB::table('hmy_user')->where('id',$user_id)->find();


        // 查询是否已关注
        $follow = Db::table(['follow' =>'f','hmy_user' =>'u','users_follow'=>'uf'])
                    ->where('uf.fid = f.id && '.$user_id.' = f.user_id && uf.uid = '.$sess_id)
                    ->find();

        // 查询是否已关注
        $fans = Db::table(['fans' =>'f','hmy_user' =>'u','users_fans'=>'uf'])
                    ->where('uf.fid = f.id && '.$sess_id.' = f.user_id && uf.uid = '.$user_id)
                    ->find();


        // 如果如果已经关注,操作则为取消关注
        if ($follow != null) {

            // 删除用户关注表数据
            $user_foll = Db::table('users_follow')->where('fid',$follow['fid'])->where('uid',$sess_id)->delete();

            // 删除关注表数据
            $foll = Db::table('follow')->where('id',$follow['fid'])->delete();

            // 删除用户粉丝表数据
            $user_fs = Db::table('users_fans')->where('uid',$user_id)->where('fid',$fans['fid'])->delete();

            // // 删除粉丝表数据
            $fs = Db::table('fans')->where('id',$fans['fid'])->delete();

            if ($fs && $user_fs && $foll && $user_foll) {
                         
                $info['status'] = true;
                $info['msg']    = '已取消关注!';

                return json($info);
            }

        }


        // 关注表
        $data = [
            'icon'      => $user['icon'],
            'iconname'  => $user['name'],
            'sex'       => $user['sex'],
            'user_id'   => $user_id
        ];

        // 插入获取新数据的id
        $follow_id = DB::table('follow')->insertGetId($data);


        // 粉丝表
        $fans_user = [
            'icon'      => $session['icon'],
            'iconname'  => $session['name'],
            'sex'       => $session['sex'],
            'user_id'   => $session['id']
        ];

        // 插入获取新数据的id
        $fans_res = DB::table('fans')->insertGetId($fans_user);


        // 用户关注表
        $c = [
            'uid' => $user_id,
            'fid' => $follow_id
        ];

        // 用户粉丝表
        $e = [
            'uid' => $session['id'],
            'fid' => $fans_res
        ];


        $f = DB::table('users_fans')->insert($c);
        $d = DB::table('users_follow')->insert($e);


        if ($d > 0 && $f >0){

            $info['status'] = true;
            $info['msg'] = '已加入关注!';
            return json($info);

        }else{
            $info['status'] = false;
            $info['msg'] = '关注失败!';
            return json($info);
        }

    }



    // 个人中心取消关注
    public function cancel($uid)
    {
        // session数据
        $session = Session::get();

        // 获取id
        $id = $session['hmy_user']['id'];


        // 查询关注
        $follow = Db::table(['follow' =>'f','users_follow'=>'uf'])
                    ->where('f.id = uf.fid && uf.uid = '.$id.' && f.user_id = '.$uid)
                    ->field('f.iconname,f.sex,f.icon,f.user_id,f.id')
                    ->select();


        // 关注表的ID
        $f_id = $follow[0]['id'];


        // 删除用户关注表id
        $res = Db::table('users_follow')->where($f_id.' = fid && uid = '.$id)->delete();

        // 删除关注表
        $result = Db::table('follow')->where($f_id.' = id && user_id = '.$uid)->delete();


        // 跳转
        if ($result > 0) {

            $info['status'] = true;
            $info['msg'] = '已取消关注!';

            return json($info);
        }

    }


    // 粉丝页面
    public function fanslist()
    {
        // 获取session
        $session = Session::get();

        // 获取id
        $id = $session['hmy_user']['id'];

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询粉丝
        $fans = Db::table(['fans' =>'f','hmy_user' =>'u','users_fans'=>'uf'])
                    ->where('f.id = uf.fid && u.id = uf.uid && uf.uid = '.$id)
                    ->field('f.iconname,f.sex,f.icon,u.status,f.user_id,f.black,f.id')
                    ->select();


        // 粉丝数量
        $fans_num = count($fans);

        // 返回
        return view('index@fansfollow/fans',[
            'session'   => $session,
            'category'  => $category,
            'fans'      => $fans,
            'fans_num'  => $fans_num
        ]);

    }


    // 粉丝拉黑
    public function blackList()
    {
        // 获取session
        $session = Session::get();

        // 获取id
        $id = $session['hmy_user']['id'];

        // 接收数据
        $uid = input('uid');


        // 查询粉丝
        $fans = Db::table(['fans' =>'f','users_fans'=>'uf'])
                    ->where('f.id = uf.fid && uf.uid = '.$id.' && f.user_id = '.$uid)
                    ->field('f.iconname,f.sex,f.icon,f.user_id,f.black,f.id')
                    ->find();


        // 定义数据
        $fans['black'] = $fans['black'] == '1'?'2':'1';

        // 获取粉丝id
        $fid = $fans['id'];

        // 更新数据
        $result = Db::table('fans')->where('id = '.$fid.' && user_id = '.$uid)->update($fans);


       // 跳转
       if ($result > 0) {

            if ($fans['black'] == 1) {

                $info['status'] = true;
                $info['msg']    = '拉出入黑名单!';

                return json($info);
            }
                $info['status'] = true;
                $info['msg']    = '已加入黑名单!';

                return json($info);

       }
    }


    // 查看粉丝/关注资料
    public function toview($uid)
    {

        // 获取session
        $session = Session::get();

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);

        // 查询资料
        $user = DB::table('hmy_user')->where('id',$uid)->find();


        // 查询关注
        $follow = Db::table(['follow' =>'f','hmy_user' =>'u','users_follow'=>'uf'])
                    ->where('f.id = uf.fid && u.id = uf.uid && uf.uid = '.$uid)
                    ->field('uf.fid')
                    ->select();


        // 关注数量
        $follow_num = count($follow);

        // 查询粉丝
        $fans = Db::table(['fans' =>'f','hmy_user' =>'u','users_fans'=>'uf'])
                    ->where('f.id = uf.fid && u.id = uf.uid && uf.uid = '.$uid)
                    ->field('uf.fid')
                    ->select();


        // 关注数量
        $fans_num = count($fans);


        // 查询粉丝
        $like = Db::table(['picture' =>'p','hmy_user' =>'u','like'=>'l'])
                    ->where('p.user_id = l.user_id && u.id = l.user_id && l.user_id= '.$uid)
                    ->field('l.pict_id')
                    ->select();

        // 查询收藏
        $like = Db::table('like')
                    ->where('user_id',$uid)
                    ->field('id')
                    ->select();
                    

        // 关注数量
        $like_num = count($like);


        // 关注数量
        $like = count($like);


        // 遍历分类(上传)
        $res = Db::table('category')
                    ->order('path', 'id')
                    ->select();

        // 粉丝的作品
        $list = Db::table('picture')
                    ->where('user_id ='.$uid)
                    ->limit(4)
                    ->select();


        // 返回
        return view('index@fansfollow/fansindex',[
            'session'       => $session,
            'category'      => $category,
            'follow_num'    => $follow_num,
            'fans_num'      => $fans_num,
            'like_num'      => $like,
            'list'          => $list,
            'res'           => $res,
            'user'          => $user
        ]);

    }


    // 粉丝/关注的粉丝页面
    public function viewfans($id)
    {
        // 获取session
        $session = Session::get();

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询粉丝
        $fans = Db::table(['fans' =>'f','hmy_user' =>'u','users_fans'=>'uf'])
                    ->where('f.id = uf.fid && u.id = uf.uid && uf.uid = '.$id)
                    ->field('f.iconname,f.sex,f.icon,u.status,f.user_id,f.black')
                    ->select();

        // 查询粉丝数据
        $user = Db::table('hmy_user')->where('id',$id)->find();


        // 粉丝数量
        $fans_num = count($fans);


        // 返回
        return view('index@fansfollow/viewfans',[
            'session'   => $session,
            'category'  => $category,
            'fans'      => $fans,
            'name'      => $user['name'],
            'fans_num'  => $fans_num,
        ]);
    }


    // 粉丝的关注页面
    public function viewfollow($id)
    {
        // 获取session
        $session = Session::get();


        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询关注

        $follow = Db::table(['follow' =>'f','hmy_user' =>'u','users_follow'=>'uf'])
                    ->where('f.id = uf.fid && u.id = uf.uid && uf.uid = '.$id)
                    ->field('f.iconname,f.sex,f.icon,u.status,f.user_id')
                    ->select();

        // 关注数量
        $follow_num = count($follow);

        // 返回
        return view('index@fansfollow/viewfollow',[
            'session'       => $session,
            'category'      => $category,
            'follow'        => $follow,
            'follow_num'    => $follow_num
        ]);
    }

    // 查看粉丝/关注的喜欢作品
    public function like($id)
    {
        // 获取session
        $session = Session::get();

        // 获取id
        $uid = $session['hmy_user']['id'];

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询粉丝/关注用户的收藏
        $like = Db::table(['like' =>'l','picture' =>'p','hmy_user' => 'u'])
                    ->where('l.pict_id = p.id && l.user_id = '.$id.' && u.id = '.$id)
                    ->field('p.id,p.title,u.name,p.src')
                    ->select();

        // 查询我的收藏
        $fans_like = Db::table(['like' =>'l','picture' =>'p','hmy_user' => 'u'])
                    ->where('l.pict_id = p.id && l.user_id = '.$uid.' && u.id = '.$uid)
                    ->field('p.id,p.title,u.name,p.src')
                    ->select();

        // var_dump($fans_like);

        // foreach ($fans_like as $k => $v) {

        //     // var_dump($v);
        //     foreach ($like as $value) {
        //         // var_dump($value);
        //         if ($v['src'] != $value['src']) {
        //             $like[$k]['follow'] = '已收藏';
        //         }else{
        //             $like[$k]['follow'] = '未收藏';

        //         }
        //     }
        // }

        // var_dump($my_like);
        // var_dump($like);
        // die;
        


        $user = Db::table('hmy_user')
                    ->where('id = '.$id)
                    ->find();
        // var_dump($user);

        // var_dump($like);


        // 关注数量
        $like_num = count($like);


        return view('index@fansfollow/like',[
            'session'   => $session,
            'category'  => $category,
            'like'      => $like,
            'user'      => $user,
            'like_num'  => $like_num
        ]);

    }


    // 查看粉丝/关注的作品
    public function picture($uid)
    {
        // 获取session
        $session = Session::get('hmy_user');
        
        // 获取id
        $id = $session['id'];

        // 查询分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);

        // 查询数据
        $list = Db::table(['picture' => 'p','category'=>'c'])
                    ->field('p.id,p.status,p.title,p.src,c.name')
                    ->where('c.id = p.cate_id && user_id ='.$uid)
                    ->order('p.id','desc')
                    ->select();

        // 查询数量
        $list_num = count($list);

        // 返回
        return view('index@fansfollow/picture',[
            'session'   => $session,
            'category'  => $category,
            'list'      => $list,
            'list_num'  => $list_num
        ]);

    }



}
